<template>
  <div class="flex flex-between pd-10">
    <div class="flex-1 flex flex-start mr-20">
      <template v-for="item in searchConfigs">
        <div v-if="item.needLable">{{ item.lable }}:</div>
        <el-input
          class="mr-10"
          v-if="item.type === ComponentType.Input"
          v-model="searchParams[item.prop]"
          :placeholder="item.placeholder ?? '请输入'"
          size="small"
        />
        <el-date-picker
          v-if="item.type === ComponentType.Date"
          v-model="searchParams[item.prop]"
          :type="item.dateType"
          range-separator="至"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          :placeholder="item.placeholder ?? '请选择时间'"
          size="small"
          @change="dateChange($event, item)"
        >
        </el-date-picker>
      </template>
    </div>
    <div class="flex">
      <el-button type="primary" size="small" @click="onSearch">搜索</el-button>
      <el-button type="primary" size="small" plain @click="onReset"
        >重置</el-button
      >
    </div>
  </div>
</template>
<script lang="ts" setup>
import { reactive } from "vue";
import { SearchConfig, ComponentType } from "../../type/search";
const props = defineProps<{
  searchConfigs: Array<SearchConfig>;
  searchParams: any;
}>();
const searchParams = reactive(props.searchParams);
const emits = defineEmits<{
  (e: "search"): void;
}>();
const onSearch = (): void => {
  emits("search");
};
const onReset = (): void => {
  props.searchConfigs.forEach((item: SearchConfig) => {
    if (searchParams[item.prop]) {
      delete searchParams[item.prop];
    }
    if (item.props) {
      delete searchParams[item.props[0]];
      delete searchParams[item.props[1]];
    }
  });
  emits("search");
};
const dateChange = (val: any, item: SearchConfig) => {
  if (Array.isArray(val) && item.props) {
    searchParams[item.props[0]] = new Date(val[0]).getTime();
    searchParams[item.props[1]] = new Date(val[1]).getTime();
    return;
  }
  searchParams[item.prop] = new Date(val as string).getTime();
};
</script>
<style lang="scss" scoped>
:deep(.el-input, .el-input__inner) {
  width: 180px;
}
:deep(.el-button.is-plain) {
  --el-button-hover-text-color: #409eff !important;
  --el-button-hover-bg-color: #ecf5ff !important;
  --el-button-hover-border-color: #b3d8ff !important;
  --el-button-active-text-color: #409eff !important;
  --el-button-active-bg-color: #ecf5ff !important;
  --el-button-active-border-color: #b3d8ff !important;
}
</style>
